package com.lrkj.payfee.controller;

import com.lrkj.common.annotation.Log;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.poi.ExcelUtil;
import com.lrkj.payfee.domain.ChargeNorm;
import com.lrkj.payfee.domain.RoomNorm;
import com.lrkj.payfee.domain.RuleCost;
import com.lrkj.payfee.domain.vo.PayFeeVo;
import com.lrkj.payfee.domain.vo.RoomNormVo;
import com.lrkj.payfee.factory.PayFeeFactory;
import com.lrkj.payfee.service.IChargeNormService;
import com.lrkj.payfee.service.IRoomNormService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 房屋收费标准Controller
 *
 * @author yajun_ren
 * @date 2021-07-29
 */
@RestController
@RequestMapping("/payfee/roomNorm")
public class RoomNormController extends BaseController {
    @Autowired
    private IRoomNormService roomNormService;

    @Autowired
    private PayFeeFactory payFeeFactory;

    @Autowired
    private IChargeNormService chargeNormService;


    /**
     * 查询房屋收费标准列表
     */
    @ApiOperation(value = "查询房屋收费标准列表", notes = "查询房屋收费标准列表")
    @PreAuthorize("@ss.hasPermi('payfee:roomNorm:list')")
    @GetMapping("/list")
    public TableDataInfo list(RoomNorm roomNorm) {
        startPage();
        List<RoomNorm> list = roomNormService.selectRoomNormList(roomNorm);
        //创建房屋费用使用的,目前应该是不需要了
//        if(null != list && list.size() > 0){
//            //收费标准
//            Map<String, ChargeNorm> chargeNormMap = chargeNormService.getChargeNormMap(list.get(0).getCommunityId());
//            list.stream().forEach(x->{
//                ChargeNorm chargeNorm = chargeNormMap.get(x.getNormId());
//                x.getFeeCondition().setMonthlyFee(payFeeFactory.computingFormulaMoney(x, chargeNorm));
//            });
//        }
        return getDataTable(list);
    }

    /**
     * 导出房屋收费标准列表
     */
    @ApiOperation(value = "导出房屋收费标准列表", notes = "导出房屋收费标准列表")
    @PreAuthorize("@ss.hasPermi('payfee:roomNorm:export')")
    @Log(title = "房屋收费标准", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RoomNorm roomNorm) {
        List<RoomNorm> list = roomNormService.selectRoomNormList(roomNorm);
        ExcelUtil<RoomNorm> util = new ExcelUtil<RoomNorm>(RoomNorm.class);
        return util.exportExcel(list, "房屋收费标准数据");
    }

    /**
     * 获取房屋收费标准详细信息
     */
    @ApiOperation(value = "获取房屋收费标准详细信息", notes = "获取房屋收费标准详细信息")
    @PreAuthorize("@ss.hasPermi('payfee:roomNorm:query')")
    @GetMapping(value = "/{roomNormId}")
    public AjaxResult getInfo(@PathVariable("roomNormId") String roomNormId) {
        return AjaxResult.success(roomNormService.selectRoomNormById(roomNormId));
    }

    /**
     * 新增房屋收费标准
     */
    @ApiOperation(value = "新增房屋收费标准", notes = "新增房屋收费标准")
    @PreAuthorize("@ss.hasPermi('payfee:roomNorm:add')")
    @Log(title = "房屋收费标准", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RoomNormVo roomNorm) {
        return toAjax(roomNormService.insertRoomNorm(roomNorm));
    }

    /**
     * 修改房屋收费标准
     */
    @ApiOperation(value = "修改房屋收费标准", notes = "修改房屋收费标准")
    @PreAuthorize("@ss.hasPermi('payfee:roomNorm:edit')")
    @Log(title = "房屋收费标准", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RoomNorm roomNorm)
    {
        //判断该收费标准时候生成过账单，假如生成过账单的话不允许修改
//        RuleCost ruleCost=new RuleCost();
//        ruleCost.setRoomNormId(roomNorm.getRoomNormId());
//        List<RuleCost> ruleCostList= ruleCostMapper.selectRuleCostList(ruleCost);
//        if(ruleCostList!=null && ruleCostList.size()>0){
//            throw new BaseException("该房屋收费标准已生成过费用单,请勿修改");
//        }
        return toAjax(roomNormService.updateRoomNorm(roomNorm));
    }

    /**
     * 删除房屋收费标准
     */
    @ApiOperation(value = "删除房屋收费标准", notes = "删除房屋收费标准")
    @PreAuthorize("@ss.hasPermi('payfee:roomNorm:remove')")
    @Log(title = "房屋收费标准", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roomNormIds}")
    public AjaxResult remove(@PathVariable String[] roomNormIds) {
        return toAjax(roomNormService.deleteRoomNormByIds(roomNormIds));
    }
}
